//封装渲染国内折扣页面的函数
function bindHtml(arr) {
    let html = ""
    arr.forEach((item, index) => {
        html += ` 
        <li productid=${item.productId}>
            <a>
            <div class="img">
               ${item.productImg}
            </div>
            <div class="title">
                <div class="title"> ${item.productName}</div>
            </div>
            <p class="price"> ${item.productPrice}</p>
            <p class="form">${item.productFrom}| ${item.productTime}</p>
            </a>
    </li>`
    })
    return html;
}


//请求国内折扣首页数据 渲染页面
ajax({
    url: 'http://chst.vip:1234/api/getinlanddiscount',
}).then(res => {
    let arr = res.result.splice(0, 4)
    let html = bindHtml(arr)
    info.innerHTML = html
    //实现content部分的懒加载
    let count = 4;
    window.onscroll = function () {
        let scrollHeight = document.documentElement.scrollHeight
        let scrollTop = document.documentElement.scrollTop
        let clientHeight = document.documentElement.clientHeight
        let threshold_value = scrollHeight - (scrollTop + clientHeight)
        if (threshold_value <= 100) {
            count += 4
            arr = res.result.splice(0, count)
            html = bindHtml(arr)
            info.innerHTML += html
        }
    };

    //给所有可被点击商品添加对应链接(方法一)
    [...info.children].forEach(li => {
        li.onclick = function () {
            let productid = this.getAttribute('productid')
            location.href = `discount.html?productid=${productid}&title=国内折扣`
        }
    })

    //给所有可被点击商品添加对应链接(方法二)有局限
    /*  var as = document.querySelectorAll('#product>ul>li>a')
     console.log(as);
     for (i = 0; i < as.length; i++) {
         as[i].href = 'discount.html?productid=' + arr[i].productId + '&title=国内折扣'
     } */
})
